<?php
jimport('joomla.application.component.model');
class svModelAccount extends JModel
{
	//------------ tim kiem----------------------------------
	function getAllAccount()
	{
		$query='SELECT 	 acc.*,cb.cb_ten,pb.pb_ten
			    FROM	 qlcb_account as acc,qlcb_canbo as cb,qlcb_phongban as pb,qlcb_quanlychung as qlc
			    WHERE	 acc.ac_idcanbo=cb.cb_id    
				AND		qlc.qlc_idcanbo=cb.cb_id 
				AND		qlc.qlc_idphongban=pb.pb_id			
		';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getAccount()
	{
		$query='SELECT 	 acc.*,cb.cb_ten,pb.pb_ten
		FROM	 qlcb_account as acc,qlcb_canbo as cb,qlcb_phongban as pb,qlcb_quanlychung as qlc
		WHERE	 acc.ac_idcanbo=cb.cb_id
		AND		qlc.qlc_idcanbo=cb.cb_id
		AND		qlc.qlc_idphongban=pb.pb_id
		ORDER BY acc.ac_id  DESC  
		LIMIT	1 ';
		$result=$this->_loadObject($query);
		return $result;
	}
	function getAccountByID($idaccount)
	{
		$query='SELECT 	 acc.*,cb.cb_ten
				FROM	 qlcb_account as acc,qlcb_canbo as cb
				WHERE	 acc.ac_idcanbo=cb.cb_id 
				AND		acc.ac_id='.$idaccount;
		$result=$this->_loadObject($query);
		return $result;
	}
	function getDonviquanlyList()
	{
		$query='SELECT *
		FROM qlcb_donviquanly
		WHERE 1';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getPhongbanList($iddonvi)
	{
		$cmd='1';
		if($iddonvi!=0)
		{
			$cmd=' pb_iddonviquanly='.$iddonvi;
		}
		$query='SELECT pb_id,pb_ten
		FROM qlcb_phongban
		WHERE '.$cmd;
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getCanboList($iddonvi,$idphongban)
	{
			$query='SELECT cb_id,cb_ten
			FROM qlcb_canbo
			WHERE 1';
		if($iddonvi!=0)
		{
			$query='SELECT  cb.cb_id,cb.cb_ten
					FROM    qlcb_canbo as cb,qlcb_phongban as pb,qlcb_quanlychung as qlc  
					WHERE	qlc.qlc_idcanbo=cb.cb_id  
					AND		qlc.qlc_idphongban=pb.pb_id 
					AND		pb.pb_iddonviquanly='.$iddonvi;
		}
		if($idphongban!=0)
		{
			$query='SELECT  cb.cb_id,cb.cb_ten
					FROM    qlcb_canbo as cb,qlcb_quanlychung as qlc  
					WHERE	qlc.qlc_idcanbo=cb.cb_id  
					AND		qlc.qlc_idphongban='.$idphongban;
		}
		$result=$this->_loadObjectList($query);
		return $result;
	}
	//----------------------------them ,sua ,xoa -------------------------------
	function addNewAccount($idcanbo,$username,$password,$email,$typeaccount)
	{
		$query="INSERT INTO qlcb_account(ac_idcanbo,ac_username,ac_password,ac_email,ac_typeaccount)
		 VALUES('".$idcanbo."','".$username."','".$password."','".$email."','".$typeaccount."')";
		$message='Thêm mới ';
		$result=$this->_Execute($query, $message);		
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	// sua
	function editAccountt($idaccount,$idcanbo,$username,$password,$email,$typeaccount)
	{
		$query="UPDATE qlcb_account SET ac_idcanbo='".$idcanbo."',ac_username='".$username."'
									,ac_email='".$email."',ac_password='".$password."',ac_typeaccount='".$typeaccount."'
	   		    WHERE ac_id=".$idaccount;
		$message='Chỉnh sửa ';
		$result=$this->_Execute($query, $message);
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	function delAccount($idaccount)
	{
		$query="DELETE FROM qlcb_account WHERE ac_id=".$idaccount;
		$message='Xóa 1 item ';
		$result=$this->_Execute($query, $message);
		return '<b style="font-size:18px;color:blue;">'.$result.'</b>';	
	}
	// cac ham phuc vu truy van CSDL
	function _loadObject($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObject();
		return $result;
	}
	function _loadObjectList($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObjectList();
		return $result;
	}
	function _Execute($query,$message)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		try {
			$db->query();
			return $message." thành công ";
		}
		catch (Exception $e){
			return $message." Không thành công! Vui lòng thử lại.";
		}
	}
}
?>